package com.dodo.videoEdit.filetool;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.dodo.videoEdit.MyApplication;
import com.dodo.videoEdit.greendao.FileDirType;
import com.dodo.videoEdit.greendao.FileDirTypeDao;
import com.dodo.videoEdit.greendao.Image;
import com.dodo.videoEdit.greendao.ImageDao;
import com.dodo.videoEdit.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoubo
 * @date 2020/8/11
 * @describe 1
 */
public class ImageProvider implements AbstructProvider {
    private Context context;

    public ImageProvider(Context context) {
        this.context = context;
    }

    @Override
    public List<?> getList() {

        ImageDao imageDao = MyApplication.getAppInstance().getDaoSession().getImageDao();
        imageDao.deleteAll();
        FileDirTypeDao fileDirTypeDao = MyApplication.getAppInstance().getDaoSession().getFileDirTypeDao();
        List<Image> list = null;
        if (context != null) {
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
                    null, null);
            if (cursor != null) {
                list = new ArrayList<Image>();
                while (cursor.moveToNext()) {
                    int id = cursor
                            .getInt(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media._ID));
                    String title = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
                    String path = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    String displayName = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
                    String mimeType = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
                    long size = cursor
                            .getLong(cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
                    Image audio = new Image(id, title, displayName, mimeType,
                            path,  FileUtil.getFilePath(path),size);
                    imageDao.insert(audio);
                     List<FileDirType> fileDirTypes = MyApplication.getAppInstance().getDaoSession().queryRaw(FileDirType.class, "where path_dir = ?", audio.getParentDir());
                    if (fileDirTypes!=null&&fileDirTypes.size()>0){
                        FileDirType fileDirType = fileDirTypes.get(0);
                        fileDirType.setIsImageDir(true);
                        fileDirType.setImageCount(fileDirType.getImageCount()+1);
                        MyApplication.getAppInstance().getDaoSession().insertOrReplace(fileDirType);
                    }else{
                        FileDirType fileDirType = new FileDirType(id,false,true,false,1,0,0,audio.getParentDir());
                        MyApplication.getAppInstance().getDaoSession().insertOrReplace(fileDirType);
                    }
                    list.add(audio);
                }
                cursor.close();
            }
        }
        return list;
    }

}